package top.drinyeit.nongmao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.model.CategoryGoodsDetailsBean;

/**
 * Created by wu on 2018/1/9.
 */

public class StoreAllAdapter extends RecyclerView.Adapter<StoreAllAdapter.ViewHolder>{
    private Context context;
    private List<CategoryGoodsDetailsBean> list;

    public StoreAllAdapter(Context context,List<CategoryGoodsDetailsBean> list){
        this.list = list;
        this.context = context;
    }

    public void clear(){
        this.list.clear();
    }

    public void setData(List<CategoryGoodsDetailsBean> list){
        this.list = list;
    }

    public void addData(List<CategoryGoodsDetailsBean> list){
        this.list.addAll(list);
    }

    public List<CategoryGoodsDetailsBean> getData(){
        return list;
    }


    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(StoreAllAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public StoreAllAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.item_category_detail,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(StoreAllAdapter.ViewHolder holder, int position) {
        Glide.with(context).load(list.get(position).getCover_img_url())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(R.drawable.ic_def)
                .dontTransform()
                .into(holder.shop_im);
        holder.commodity_name.setText(list.get(position).getCommodity_name());
        holder.price.setText(list.get(position).getSale_price());
        holder.shop_name.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public ImageView shop_im;
        public TextView commodity_name;
        public TextView shop_name;
        public TextView price;
        public ImageView buy;

        public ViewHolder(View itemView) {
            super(itemView);
            shop_im = itemView.findViewById(R.id.shop_im);
            commodity_name = itemView.findViewById(R.id.commodity_name);
            shop_name = itemView.findViewById(R.id.shop_name);
            price = itemView.findViewById(R.id.price);
            buy = itemView.findViewById(R.id.buy);

            itemView.setOnClickListener(this);
            buy.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null){
                itemClickListener.onItemClick(v, getAdapterPosition());
                switch (v.getId()){
                    case R.id.buy:
                        break;
                }
            }
        }
    }
}
